#library(fGarch)
#library(fArma)
"arma.garch.sim"=function(phis,d,thetas,mu,alphas,betas,omega,n,outliers, cond.distn,shape){
p=ifelse(length(na.omit(phis))==0,0,length(phis))
q=ifelse(length(na.omit(thetas))==0,0,length(thetas))
r=ifelse(length(na.omit(alphas))==0,0,length(alphas))
s=ifelse(length(na.omit(betas))==0,0,length(betas))
phis[which(is.na(phis))]=0
thetas[which(is.na(thetas))]=0
alphas[which(is.na(alphas))]=0
betas[which(is.na(betas))]=0
if(r==0 & s > 0){
stop("You must have at least order 1 in the autoregressive (alpha) coefficients")
}
if (r==0 & s==0){ #just doing an ARMA sim here
		if (cond.distn=="norm"){
			sim.values =  as.data.frame(armaSim(model = list(ar = phis, d = d, ma = thetas), n = n,
			innov = NULL, n.start = 100, start.innov = NULL, rand.gen = rnorm, 
			rseed = NULL, addControl = FALSE))$TS.1
		}else{
			if(cond.distn=="std"){
			sim.values =  as.data.frame(armaSim(model = list(ar = phis, d = d, ma = thetas), n = n,
			innov = NULL, n.start = 100, start.innov = NULL, rand.gen = rstd, 
			rseed = NULL, addControl = FALSE,nu=shape))$TS.1
			}else{
				stop("unsupported conditional distrubtion");
				}
		}
}else{
	garchSpec = garchSpec(model = list(ar = phis, ma = thetas, alpha = alphas, beta = betas, mu=mu,omega=omega), cond.dist = cond.distn,shape = shape)
	sim.values = as.data.frame(garchSim(spec = garchSpec(), n = n, n.start = 100, extended = FALSE))$garch
}
return (structure(list (sim.values=sim.values, p=p,q=q,r=r,s=s)))
}